package com.olda.play.system.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.olda.play.enums.ConstantEnum;
import com.olda.play.rpc.CommonResponseDto;
import com.olda.play.system.biz.SysDeptBizService;
import com.olda.play.system.biz.SysRoleBizService;
import com.olda.play.system.biz.SysUserBizService;
import com.olda.play.system.biz.dao.SysUserBizMapper;
import com.olda.play.system.dto.SysUserDto;
import com.olda.play.system.model.dao.SysUserMapper;
import com.olda.play.system.model.po.SysDept;
import com.olda.play.system.model.po.SysRole;
import com.olda.play.system.model.po.SysUser;
import com.olda.play.system.model.po.SysUserExample;
import com.olda.play.system.vo.SysUserVo;
import com.olda.play.utils.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 */
@Service
public class SysUserBizServiceImpl implements SysUserBizService {


    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysUserBizMapper sysUserBizMapper;
    @Autowired
    private SysDeptBizService sysDeptBizService;
    @Autowired
    private SysRoleBizService sysRoleBizService;

    @Override
    public SysUserDto selectSysUserByUserName(String userName) {
        SysUserExample sysUserExample=new SysUserExample();
        SysUserExample.Criteria criteria=sysUserExample.createCriteria();
        criteria.andUserNameEqualTo(userName);
        List<SysUser> sysUserList=sysUserMapper.selectByExample(sysUserExample);
        if(sysUserList.size()==0){
            return null;
        }
        SysUser sysUser=sysUserList.get(0);
        SysUserDto sysUserDto= BeanUtil.copyProperties(sysUser, SysUserDto.class);
        packSysUser(sysUserDto);
        return sysUserDto;
    }

    /**
     * 分页获取用户数据
     * @param param
     * @return
     */
    @Override
    public CommonResponseDto<List<SysUserDto>> querySysUserList(SysUserVo param) {
        Map<String,Object> map= BeanUtil.copyProperties(param, Map.class);
        map.put("isDeleted", ConstantEnum.IS_DELETE_NO.getCode());
        int pageNum = param.getPageNum();
        int pageSize = param.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<SysUserDto> list=sysUserBizMapper.selectByCondition(map);
        if(list.size()>0){
            list.forEach(item ->{
                packSysUser(item);
            });
        }
        PageInfo<SysUserDto> pageInfo = new PageInfo(list);
        int count = Integer.parseInt(String.valueOf(pageInfo.getTotal()));
        if (CollectionUtils.isEmpty(list)) {
            return CommonResponseDto.success(new PageInfo<>(new ArrayList<>()));
        }
        return CommonResponseDto.success(pageInfo);
    }

    /**
     * 包装返回的用户信息
     * @param sysUserDto
     * @return
     */
    private SysUserDto packSysUser(SysUserDto sysUserDto) {
        //1、查询用户角色信息
        List<SysRole> sysRoles=sysRoleBizService.getSysRoleByUserId(sysUserDto.getId());
        if(sysRoles.size()>0){
            SysRole sysRole=sysRoles.get(0);
            sysUserDto.setRoleId(sysRole.getId());
            sysUserDto.setRoleCode(sysRole.getCode());
            sysUserDto.setRoleName(sysRole.getName());
        }
        //2、查询用户对应的部门名称
        SysDept sysDept= sysDeptBizService.selectSysDeptById(sysUserDto.getDeptId());
        if(sysDept!=null){
            sysUserDto.setDeptName(sysDept.getName());
        }
        //3、用户敏感信息脱敏返回
        sysUserDto.setMobile(EncryptUtil.mobileEncrypt(sysUserDto.getMobile()));
        sysUserDto.setUserEmail(EncryptUtil.emailEncrypt(sysUserDto.getUserEmail()));
        sysUserDto.setIdNumber(EncryptUtil.idEncrypt(sysUserDto.getIdNumber()));
        return sysUserDto;
    }

    /**
     * 根据id查询用户详情
     * @param id
     * @return
     */
    @Override
    public SysUserDto selectSysUserById(Long id) {
        SysUser sysUser=sysUserMapper.selectByPrimaryKey(id);
        SysUserDto sysUserDto=BeanUtil.copyProperties(sysUser,SysUserDto.class);
        packSysUser(sysUserDto);
        return sysUserDto;
    }
}
